package com.cmrx.model.service.impl;

import com.cmrx.model.dao.CommonPictureDao;
import com.cmrx.model.dao.InvestigationDao;
import com.cmrx.model.dao.PictureThumbnailDao;
import com.cmrx.model.dao.SceneVideoEvidenceDao;
import com.cmrx.model.entity.*;
import com.cmrx.model.service.CommonPictureServer;
import com.cmrx.model.service.SceneVideoEvidenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
@Transactional
public class SceneVideoEvidenceServiceImpl implements SceneVideoEvidenceService {
    @Autowired
    private SceneVideoEvidenceDao sceneVideoEvidenceDao;

    @Autowired
    private CommonPictureDao commonPictureDao;

    @Autowired
    private PictureThumbnailDao pictureThumbnailDao;

    @Autowired
    private CommonPictureServer commonPictureServer;

    @Autowired
    private InvestigationDao investigationDao;

    @Override
    public List<SceneVideoEvidence> queryByInvestigationId(String investigationId) {
        List<SceneVideoEvidence> videos = sceneVideoEvidenceDao.queryByInvestigationId(investigationId);
        List<SceneVideoEvidence> list = new ArrayList<>();
        for(SceneVideoEvidence video:videos) {
            CommonPictureThumbnail com = pictureThumbnailDao.queryByPictureId(video.getEvidencePhotoId());
            video.setContent(com.getContent());
            list.add(video);
        }
        return list;
    }


    //根据现勘id获取视听物证统计
    @Override
    public Integer queryCount(String investigationId) {
        return sceneVideoEvidenceDao.queryCount(investigationId);
    }

    //根据id查找视听物证信息
    @Override
    public SceneVideoEvidence queryById(String id) {
        SceneVideoEvidence video = sceneVideoEvidenceDao.queryById(id);
        if(video!=null) {
            CommonPicture com = commonPictureDao.queryById(video.getEvidencePhotoId());
            video.setContent(com.getContent());
        }
        return video;
    }

    //添加视听物证信息
    @Override
    public String addVideoEvidence(SceneVideoEvidence videoEvidence, User user,CommonPicture commonPicture) {
        commonPicture.setInvestigationId(videoEvidence.getInvestigationId());
        commonPicture.setContent(videoEvidence.getContent());
        commonPicture.setCategory("13");
        String pictureId = commonPictureServer.AddPicture(user,commonPicture);
        int serialNo = 1;
        Integer no = sceneVideoEvidenceDao.queryMaxNo(videoEvidence.getInvestigationId());
        if(no!=null){
            serialNo = no+1;
        }
        videoEvidence.setCreateUserId(user.getUserid());
        videoEvidence.setCreateUser(user.getUserName());
        videoEvidence.setEvidencePhotoId(pictureId);
        videoEvidence.setId(UUID.randomUUID().toString().replaceAll("-",""));
        videoEvidence.setSerialNo(serialNo+"");
        int count = sceneVideoEvidenceDao.addVideoEvidence(videoEvidence);
        if(count>0) {
            return videoEvidence.getId();
        }else{
            return "";
        }
    }

    //根据id修改视听物证信息
    @Override
    public int updateById(SceneVideoEvidence videoEvidence,User user) {
        SceneVideoEvidence video = sceneVideoEvidenceDao.queryById(videoEvidence.getId());
        commonPictureServer.updatePicture(user,video.getEvidencePhotoId(),videoEvidence.getContent());
        videoEvidence.setUpdateUser(user.getUserName());
        int count = sceneVideoEvidenceDao.updateById(videoEvidence);
        return count;
    }

    //根据id删除视听物证信息
    @Override
    public int deleteById(String id) {
        SceneVideoEvidence video = sceneVideoEvidenceDao.queryById(id);
        if(video!=null) {
            pictureThumbnailDao.deleteById(video.getEvidencePhotoId());
            commonPictureDao.deleteById(video.getEvidencePhotoId());
        }
        int result = sceneVideoEvidenceDao.deleteById(id);
        return result;
    }
}
